package com.yiyahui.project.erp.service.impl;

import java.util.List;
import com.yiyahui.common.utils.DateUtils;
import com.yiyahui.project.erp.domain.YiyaMarketReservation;
import com.yiyahui.project.erp.domain.requestVo.YiyaMarketVisitRequest;
import com.yiyahui.project.erp.service.IYiyaMarketReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiyahui.project.erp.mapper.YiyaMarketVisitMapper;
import com.yiyahui.project.erp.domain.YiyaMarketVisit;
import com.yiyahui.project.erp.service.IYiyaMarketVisitService;
import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.util.DateUtil.now;

/**
 * 市场回访Service业务层处理
 * 
 * @author yiyahui
 * @date 2020-05-08
 */
@Service
public class YiyaMarketVisitServiceImpl implements IYiyaMarketVisitService 
{
    @Autowired
    private YiyaMarketVisitMapper yiyaMarketVisitMapper;

    @Autowired
    private IYiyaMarketReservationService yiyaMarketReservationService;

    /**
     * 查询市场回访
     * 
     * @param id 市场回访ID
     * @return 市场回访
     */
    @Override
    public YiyaMarketVisit selectYiyaMarketVisitById(Long id)
    {
        return yiyaMarketVisitMapper.selectYiyaMarketVisitById(id);
    }

    /**
     * 查询市场回访列表
     * 
     * @param yiyaMarketVisitRequest 市场回访
     * @return 市场回访
     */
    @Override
    public List<YiyaMarketVisit> selectYiyaMarketVisitList(YiyaMarketVisitRequest yiyaMarketVisitRequest)
    {
        return yiyaMarketVisitMapper.selectYiyaMarketVisitList(yiyaMarketVisitRequest);
    }

    /**
     * 新增市场回访
     * 
     * @param yiyaMarketVisit 市场回访
     * @return 结果
     */
    @Transactional(rollbackFor=Exception.class)
    public int insertYiyaMarketVisit(YiyaMarketVisit yiyaMarketVisit)
    {
        yiyaMarketVisit.setCreateTime(DateUtils.getNowDate());
        int i = yiyaMarketVisitMapper.insertYiyaMarketVisit(yiyaMarketVisit);
        YiyaMarketReservation yiyaMarketReservationRequest = new YiyaMarketReservation();
        yiyaMarketReservationRequest.setId(yiyaMarketVisit.getMarketReservationId());
        yiyaMarketReservationRequest.setReach(DateUtils.getTime()+" ["+yiyaMarketVisit.getCreateBy()+"]"+": "+yiyaMarketVisit.getResult()+";\n");
        yiyaMarketReservationRequest.setFollowPerson(yiyaMarketVisit.getFollowPerson());
        yiyaMarketReservationRequest.setFinalVisitTime(yiyaMarketVisit.getVisitTime());
        yiyaMarketReservationRequest.setReservationVisitTime(yiyaMarketVisit.getNextVisitTime());
        yiyaMarketReservationRequest.setCallBackState(yiyaMarketVisit.getFollowStatus());
        yiyaMarketReservationRequest.setUpdateBy(yiyaMarketVisit.getCreateBy());
        yiyaMarketReservationService.updateYiyaMarketReservation(yiyaMarketReservationRequest);
        return i;
    }

    /**
     * 修改市场回访
     * 
     * @param yiyaMarketVisit 市场回访
     * @return 结果
     */
    @Override
    public int updateYiyaMarketVisit(YiyaMarketVisit yiyaMarketVisit)
    {
        yiyaMarketVisit.setUpdateTime(DateUtils.getNowDate());
        return yiyaMarketVisitMapper.updateYiyaMarketVisit(yiyaMarketVisit);
    }

    /**
     * 批量删除市场回访
     * 
     * @param ids 需要删除的市场回访ID
     * @return 结果
     */
    @Override
    public int deleteYiyaMarketVisitByIds(Long[] ids)
    {
        return yiyaMarketVisitMapper.deleteYiyaMarketVisitByIds(ids);
    }

    /**
     * 删除市场回访信息
     * 
     * @param id 市场回访ID
     * @return 结果
     */
    @Override
    public int deleteYiyaMarketVisitById(Long id)
    {
        return yiyaMarketVisitMapper.deleteYiyaMarketVisitById(id);
    }
}
